package com.sansecy.monitor.see;

import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class SeePluginLoader {
    private static final String TAG = "SeePluginLoader-App";

    private static List<Pair<CharSequence, Runnable>> appPlugins = new ArrayList<>();

    private static String pluginsPackageName = "com.sansecy.plugins";

    public static List<Pair<CharSequence, Runnable>> getAppPlugins() {
        loadPlugins();
        return appPlugins;
    }

    private static void loadPlugins() {
        try {
            Class<?> pluginManager = Class.forName(pluginsPackageName + ".PluginManager");
            List<String> pluginNames = (List<String>) pluginManager.getDeclaredMethod("getPluginNames").invoke(null);
            for (String name : pluginNames) {
                Class<?> aClass = Class.forName(pluginsPackageName + "." + name);
                Object o = aClass.getDeclaredMethod("init").invoke(null);
                appPlugins.add((Pair<CharSequence, Runnable>) o);
            }
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            Log.e(TAG, "loadPlugins: ", e);
        }
    }

    public static void setPluginsPackageName(String paramString) {
        if (!TextUtils.isEmpty(paramString))
            pluginsPackageName = paramString;
    }

}

